#ifndef MAINWINDOW_H
#define MAINWINDOW_H

#include <QMainWindow>
#include <QFileDialog>
#include <QProgressBar>
#include <QLabel>
#include <QPushButton>
#include <QLineEdit>
#include <QCheckBox>
#include <QSlider>
#include <QSpinBox>
#include <QDoubleSpinBox>
#include <QGroupBox>
#include <QVBoxLayout>
#include <QHBoxLayout>
#include <QGridLayout>
#include <QTextEdit>
#include <QMessageBox>
#include <QTabWidget>
#include <QScrollArea>
#include <QAction>
#include <QMenuBar>
#include <QStatusBar>
#include <QInputDialog>
#include <QWidget>
#include "modeloptimizer.h"
#include "modelpreview.h"
#include "logdialog.h"
#include <QListWidget>
#include <QThread> // Added for QThread

QT_BEGIN_NAMESPACE
namespace Ui { class MainWindow; }
QT_END_NAMESPACE

class ModelLoaderThread : public QThread {
    Q_OBJECT
public:
    // 从内存加载的构造函数
    ModelLoaderThread(const QByteArray& fileData, const QString& fileName, unsigned int flags, QObject* parent = nullptr);
    // 从文件加载的构造函数（保持兼容性）
    ModelLoaderThread(const QString& filePath, unsigned int flags, QObject* parent = nullptr);
    void run() override;
signals:
    void modelLoaded(const aiScene* scene, Assimp::Importer* importer, const QString& filePath);
    void loadError(const QString& error);
private:
    QString m_filePath;
    QByteArray m_fileData;  // 文件数据
    QString m_fileName;      // 文件名
    unsigned int m_flags;
    bool m_useMemory;        // 是否使用内存加载
};

class MainWindow : public QMainWindow
{
    Q_OBJECT

public:
    MainWindow(QWidget *parent = nullptr);
    ~MainWindow();

private slots:
    void onSelectInputFile();
    void onSelectOutputFile();
    void onStartOptimization();
    void onStopOptimization();
    void onProgressUpdated(int percentage);
    void onOptimizationCompleted(const ModelOptimizer::OptimizationResult &result);
    void onErrorOccurred(const QString &error);
    void onOpenModel();

    void onAbout();
    void onPreviewModel();
    void onShowLogDialog();    // 显示日志对话框
    void onExit();             // 退出应用程序
    
    // 新增槽函数
    void onSelectModelOrFolder();
    void onSelectSingleModel();
    void onSelectModelFolder();
    void onInputPreviewClicked();    // 输入模型预览按钮槽函数
    void onOutputPreviewClicked();   // 输出路径预览按钮槽函数
    
    // 缩放控制槽函数
    void onScaleSliderChanged(int value);
    void onScaleSpinBoxChanged(double value);
    void onResetScaleClicked();
    void onResetViewClicked();
    void onModelPreviewScaleChanged(float scale);

    // 动画控制槽函数
    void onAnimationListChanged();
    void onAnimationStateChanged(int animationIndex, bool isPlaying);
    void onPlayAnimationClicked();
    void onStopAnimationClicked();
    void onPauseAnimationClicked();
    void onResumeAnimationClicked();
    void onAnimationListItemDoubleClicked(QListWidgetItem *item);
    
    // 视图控制槽函数
    void onFrontViewClicked();
    void onBackViewClicked();
    void onLeftViewClicked();
    void onRightViewClicked();
    void onTopViewClicked();
    void onBottomViewClicked();
    void onRotationSensitivityChanged(int value);
    void onAutoAdjustCameraChanged(bool enabled);

    void onModelLoaded(const aiScene* scene, Assimp::Importer* importer, const QString& filePath);
    void onModelLoadError(const QString& error);

private:
    void setupUI();
    void setupMenuBar();
    void createAnimationControls();
    
    void loadModelsFromFolder(const QString &folderPath);
    void previewModel(const QString &filePath);
    void previewModelFromMemory(const QByteArray& fileData, const QString& fileName);
    void previewModelFromFileToMemory(const QString& filePath);

    void enableControls(bool enable);

    Ui::MainWindow *ui;
    ModelOptimizer *m_optimizer;
    ModelPreview *m_modelPreview;
    QList<Assimp::Importer*> m_importers; // 存储Importer对象用于预览
    
    // 菜单动作
    QAction *m_actionOpen;
    QAction *m_actionExit;
    QAction *m_actionAbout;
    QAction *m_actionPreview;
    QAction *m_actionSelectModel; // 模型选择动作
    QAction *m_actionShowLog;     // 查看日志动作
    
    // 文件选择控件
    QLineEdit *m_inputFileEdit;
    QLineEdit *m_outputFileEdit;
    QPushButton *m_inputFileButton;
    QPushButton *m_outputFileButton;
    QPushButton *m_inputPreviewButton;   // 输入模型预览按钮
    QPushButton *m_outputPreviewButton;  // 输出目录预览按钮
    
    // 进度控制控件
    QPushButton *m_startButton;
    QPushButton *m_stopButton;
    QProgressBar *m_progressBar;
    QLabel *m_progressPercentLabel;
    
    // 基础优化选项控件
    QDoubleSpinBox *m_vertexReductionSpinBox;
    QDoubleSpinBox *m_normalToleranceSpinBox;
    QDoubleSpinBox *m_uvToleranceSpinBox;
    
    // 网格优化选项控件
    QCheckBox *m_removeUnusedVerticesCheckBox;
    QCheckBox *m_joinIdenticalVerticesCheckBox;

    QCheckBox *m_triangulateCheckBox;
    QCheckBox *m_improveCacheLocalityCheckBox;
    QCheckBox *m_optimizeMeshesCheckBox;
    
    // 法线处理选项控件
    QCheckBox *m_generateNormalsCheckBox;
    QCheckBox *m_generateSmoothNormalsCheckBox;
    QCheckBox *m_calculateTangentSpaceCheckBox;
    QCheckBox *m_fixInfacingNormalsCheckBox;
    
    // 材质优化选项控件
    QCheckBox *m_removeRedundantMaterialsCheckBox;
    QCheckBox *m_embedTexturesCheckBox;
    
    // 数据验证选项控件
    QCheckBox *m_findDegeneratesCheckBox;
    QCheckBox *m_findInvalidDataCheckBox;
    QCheckBox *m_validateDataStructureCheckBox;
    QCheckBox *m_optimizeGraphCheckBox;
    
    // 高级选项控件
    QCheckBox *m_sortByPTypeCheckBox;
    QCheckBox *m_preTransformVerticesCheckBox;
    QCheckBox *m_splitLargeMeshesCheckBox;
    QCheckBox *m_limitBoneWeightsCheckBox;
    QCheckBox *m_flipUVsCheckBox;
    QCheckBox *m_flipWindingOrderCheckBox;
    QCheckBox *m_splitByBoneCountCheckBox;
    QCheckBox *m_deboneCheckBox;
    QCheckBox *m_forceGenNormalsCheckBox;
    QCheckBox *m_dropNormalsCheckBox;
    QCheckBox *m_genUVCoordsCheckBox;
    QCheckBox *m_transformUVCoordsCheckBox;
    QCheckBox *m_findInstancesCheckBox;
    QCheckBox *m_reverseWindingOrderCheckBox;
    QCheckBox *m_invertNormalsCheckBox;
    
    // 日志控件
    LogDialog *m_logDialog;
    
    // 模型预览缩放控制
    QSlider *m_scaleSlider;
    QDoubleSpinBox *m_scaleSpinBox;
    QPushButton *m_resetScaleButton;
    QPushButton *m_resetViewButton;
    QLabel *m_scaleLabel;

    // 动画控制相关 - 现在在UI文件中定义
};

#endif // MAINWINDOW_H
